package com.fhz.day0006;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.List;

/**
 * 时间:2022/4/23
 *
 * @author msfeng
 * 简述: TODO
 */
public class MyAdapter extends PagerAdapter {

    private List<String> list;
    private Context context;

    public MyAdapter(List<String> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {

        View inflate = LayoutInflater.from(context).inflate(R.layout.vp_layout, null);

        //内容
        TextView textView = inflate.findViewById(R.id.vp_tv);
        textView.setText(list.get(position));

        //添加一个布局,不加,页面上没有效果.也不报错.
        container.addView(inflate);

        return inflate;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//        super.destroyItem(container, position, object);
        container.removeView((View) object);
    }
}

